﻿
Partial Class WebForm_ManageProgram_SearchProgram
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.BindDDLParentProgram()
            RaiseEvent Loaded(Me, New EventArgs)

        End If
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand
        Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)

        If e.CommandName = "lknUploadHelp" Then
            Me.UploadFileControl1.strType = Me.gv.DataKeys(row.RowIndex)("ProgramID")
            Me.UploadFileControl1.InitDataAndShow()
        End If
    End Sub

    Protected Sub gv_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gv.RowUpdating
        If Me.SaveData(e.RowIndex) = True Then
            Me.BindGV()
            MyObj.ShowMessage(Me.GetLocalResourceObject("SaveData.SuccessMessage"))
        End If
    End Sub

    Protected Sub gv_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gv.Sorting
        Me.BindGV()
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.BindGV()
    End Sub

    Protected Sub UploadFileControl1_Uploaded(ByVal sender As Object, ByVal e As WebForm_CommonControl_UploadFileControl.UploadedEventArgs) Handles UploadFileControl1.Uploaded
        'Save document path
        'Dim dat As New clsData
        'Dim sql As New MyStringBuilder

        'Using scope As New Transactions.TransactionScope
        '    sql.AppendFormat("update Program set DocumentPathID = '{0}' ", MyObj.GetUploadPath(e.UploadID))
        '    sql.AppendFormat("where ProgramID = {0} ", e.RecordID)

        '    dat.AccData(sql)

        '    scope.Complete()
        'End Using

        'Me.BindGV()
    End Sub
End Class

Partial Class WebForm_ManageProgram_SearchProgram
    Private Sub BindDDLParentProgram()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select p.* ")
        sql.AppendFormat("from Program p ")
        sql.AppendFormat("where p.Parent=0 ")

        Dim builder As New SelectStringBuilder("Program p")

        builder.AddColumn("p.*")
        builder.AddCondition("Parent", MyEqualityOperator.Equal, 0)
        'builder.OrCondition("Parent", MyEqualityOperator.Equal, New MyVariableExpression("Parent1"))
        'builder.AddWithValue("Parent1", 1)

        'builder.OrCondition("Active", MyEqualityOperator.NotEqual, "Y")

        Me.ddlProgramGroup.DataTextField = "ProgramName"
        Me.ddlProgramGroup.DataValueField = "ProgramID"
        Me.ddlProgramGroup.DataSource = dat.GetData(builder)
        Me.ddlProgramGroup.DataBind()

        Me.ddlProgramGroup.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub

    Public Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select p.*, p_parent.ProgramName as ParentProgramName ")
        sql.AppendFormat("from Program p ")
        sql.AppendFormat("inner join Program p_parent on p.Parent = p_parent.ProgramID ")
        sql.AppendFormat("where p.Editable='Y' ")
        sql.AppendFormat(Me.GetWhereSQL())
        sql.AppendFormat("order by p_parent.ProgramID ")
        sql.AppendFormat("," + Me.gv.SortSQL)

        Dim builder As New SelectStringBuilder("Program p")

        builder.AddColumn("p.*, ParentProgramName = p_parent.ProgramName ")
        builder.LeftJoin("Program p_parent", "p.Parent", "p_parent.ProgramID")
        builder.AddCondition("p.Editable", "Y")
        builder.AddCondition(Me.GetWhereSQL2())

        'MyObj.WriteLog(builder.ToString())

        'builder.AddCondition("Parent", MyEqualityOperator.Equal, 0)
        'builder.OrCondition("Parent", MyEqualityOperator.Equal, New MyVariableExpression("Parent1"))
        'builder.AddWithValue("Parent1", 1)

        'builder.OrCondition("Active", MyEqualityOperator.NotEqual, "Y")
        Me.gv.DataSource = dat.GetData(builder)
        Me.gv.DataBind()
    End Sub

    Private Function GetWhereSQL() As String
        Dim sql As New MyStringBuilder

        If Me.ddlProgramGroup.SelectedValue <> "" Then
            sql.AppendFormat("and p.Parent = {0} ", Me.ddlProgramGroup.SelectedValue)
        End If

        Return sql.ToString()
    End Function

    Private Function GetWhereSQL2() As ConditionStringBuilder
        Dim sql As New ConditionStringBuilder

        If Me.ddlProgramGroup.SelectedValue <> "" Then
            sql.AddVarCondition("p.Parent", "Parent")
            sql.AddWithValue("Parent", Me.ddlProgramGroup.SelectedValue)
        End If

        Return sql
    End Function

    Protected Function SaveData(ByVal rowIndex As Integer) As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope
            sql.AppendFormat("update Program set ")
            sql.AppendFormat("Description=@Description ")
            sql.AppendFormat(",Visible=@Visible ")
            sql.AppendFormat("where ProgramID = @ProgramID ")

            sql.AddWithValue("Description", Me.gv.FindTxtControl(rowIndex, "txtDescription").Text)
            sql.AddWithValue("Visible", Me.gv.FindDDLControl(rowIndex, "ddlVisible").SelectedValue)
            sql.AddWithValue("ProgramID", Me.gv.DataKeys(rowIndex)("ProgramID"))

            Dim builder As New UpdateStringBuilder("Program")

            builder.AddVarAssignment("Description")
            builder.AddVarAssignment("Visible")
            builder.AddVarCondition("ProgramID", "ProgramID")

            builder.AddWithValue("Description", Me.gv.FindTxtControl(rowIndex, "txtDescription").Text)
            builder.AddWithValue("Visible", Me.gv.FindDDLControl(rowIndex, "ddlVisible").SelectedValue)
            builder.AddWithValue("ProgramID", Me.gv.DataKeys(rowIndex)("ProgramID"))

            'MyObj.WriteLog(builder.ToString())
            dat.AccData(builder)

            scope.Complete()
        End Using

        Return True
    End Function

    Public Event Loaded(ByVal source As Object, ByVal e As EventArgs)
End Class

